
/*通过mutaitions 方法间接更新 state 对象*/

import {
    RECIEVE_CATEGORYS,
    RECIEVE_ADDRESS,
    RECIEVE_SHOPS, RECIEVE_USERINFO, RESET_USERINFO,RECEIVE_RATINGS,RECEIVE_INFO,RECEIVE_GOODS
} from "./mutations-types"

import {
    reqAddress,
    reqFoodCategorys,
    reqShops, reqUserInfo,reqLogout,reqShopInfo,reqShopRatings,reqShopGoods
} from '../api'

export default {
     //异步获取地址
     async getAddress({commit,state}){  //这里的参数本来是 context，但是解构出来之后有{commit,state}之类的，第二个参数才是普通的传参
          //发送ajax
        const res = await reqAddress(state.latitude,state.longitude)
         if (res.data.code === 0){
             console.log(res);
             commit(RECIEVE_ADDRESS,res.data.data)
         }
      },
    //异步获取食品分类
    async getCategorys({commit}){  //这里的参数本来是 context，但是解构出来之后有{commit,state}之类的，第二个参数才是普通的传参
        //发送ajax
        const res = await reqFoodCategorys()
        if (res.data.code === 0){
            console.log(res.data.data);
            commit(RECIEVE_CATEGORYS,res.data.data)
        }
    },
    //异步获取商家列表
    async getShops({commit,state}){  //这里的参数本来是 context，但是解构出来之后有{commit,state}之类的，第二个参数才是普通的传参
        //发送ajax
        const res = await reqShops(state.latitude,state.longitude)
        if (res.data.code === 0){
            console.log('shops:',res);
            let result = res.data.data
            commit(RECIEVE_SHOPS, result)
        }
    },
    //异步获取登录信息
   async getUserInfo({commit}){
       const res = await reqUserInfo();
       console.log(res)
       if (res.data.code === 0){
           let result = res.data.data
           commit(RECIEVE_USERINFO, result)
       }
    },
    //异步获取登出去
    async getLogout({commit}){
        const res = await reqLogout();
        console.log(res)
        if (res.data.code === 0){
            //let result = res.data.data
            commit(RESET_USERINFO)
        }
    },

    // 异步获取商家信息
    async getShopInfo({commit}) {
        const result = await reqShopInfo()
        console.log('shopInfo:',result);
        if (result.data.code === 0) {
            const info = result.data.data
            commit(RECEIVE_INFO, info)
        }
    },

    // 异步获取商家评价列表
    async getShopRatings({commit}, callback) {
        const result = await reqShopRatings()
        if (result.code === 0) {
            const ratings = result.data
            commit(RECEIVE_RATINGS, {ratings})
            // 数据更新了, 通知一下组件
            callback && callback()
        }
    },

    // 异步获取商家商品列表
    async getShopGoods({commit}, callback) {
        const result = await reqShopGoods()
        if (result.code === 0) {
            const goods = result.data
            commit(RECEIVE_GOODS, {goods})
            // 数据更新了, 通知一下组件
            callback && callback()
        }
    },
}